package com.feng.chat.controller;

import com.feng.chat.dto.Result;
import com.feng.chat.service.ISplitUploadService;
import com.feng.chat.service.IUsersService;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.MultipartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/upload")
public class UploadController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private ISplitUploadService splitUploadService;

    /**
     * 头像上传
     *
     * @param file
     * @return
     */
    @PostMapping("/avatar")
    public Result avatar(@RequestBody MultipartFile file) {
        Result result = usersService.avatar(file);
        return result;
    }

    /**
     * 获取拆分文件信息
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/multipart/initiate")
    public Result multipartInitiate(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = splitUploadService.multipartInitiate(userId, map.get("file_name"), Long.parseLong(map.get("file_size")));
        return result;
    }

    /**
     * 文件拆分上传
     *
     * @param request
     * @param multipartVO
     * @return
     */
    @PostMapping("/multipart")
    public Result multipart(HttpServletRequest request, MultipartVO multipartVO) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = splitUploadService.multipart(userId, multipartVO.getFile(), multipartVO.getUpload_id(), multipartVO.getSplit_index(), multipartVO.getSplit_num());
        return result;
    }
}
